package leetcode.newVersion.window;

import java.util.HashSet;

public class lc3 {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.isEmpty()) {
            return 0;
        }

        HashSet<Character> set = new HashSet<>();//用set来避免重复的字符
        int ans = 0;
        int left = 0;

        for (int right = 0; right < s.length(); right++) {
            char ch = s.charAt(right);
            while (set.contains(ch)) {
                //将重复的元素从set中移除
                set.remove(s.charAt(left));
                //重复，将窗口的左指针右移一位，减小窗口大小
                left++;
            }

            set.add(ch);//不重复，将新的char放入set中
            ans = Math.max(ans, right - left + 1);//更新最大长度
        }
        return ans;
    }

    public static void main(String[] args) {
        String s1 = "abcabcbb";
        String s2 = "bbbbb";
        String s3 = "pwwkew";

        lc3 sol = new lc3();
        System.out.println(sol.lengthOfLongestSubstring(s1));
        System.out.println(sol.lengthOfLongestSubstring(s2));
        System.out.println(sol.lengthOfLongestSubstring(s3));
    }
}
